<?php
// 开启会话
session_start();

// 初始化游戏数据
if (!isset($_SESSION['racer'])) {
    $_SESSION['racer'] = [
        'fuel' => 100, // 初始油量
        'money' => 1000, // 初始金钱
        'race_results' => [] // 比赛成绩记录
    ];
}

// 定义赛道信息
$tracks = [
    'short_track' => [
        'name' => '短赛道',
        'fuel_cost' => 20, // 油量消耗
        'prize' => 300, // 获胜奖金
        'difficulty' => 30 // 比赛难度（0 - 100）
    ],
    'medium_track' => [
        'name' => '中赛道',
        'fuel_cost' => 40,
        'prize' => 600,
        'difficulty' => 50
    ],
    'long_track' => [
        'name' => '长赛道',
        'fuel_cost' => 60,
        'prize' => 900,
        'difficulty' => 70
    ]
];

// 处理比赛请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['track'])) {
    $selected_track = $_POST['track'];
    $track = $tracks[$selected_track];

    // 检查油量是否足够
    if ($_SESSION['racer']['fuel'] >= $track['fuel_cost']) {
        $_SESSION['racer']['fuel'] -= $track['fuel_cost'];

        // 模拟比赛结果
        $random_result = rand(1, 100);
        if ($random_result > $track['difficulty']) {
            // 比赛获胜
            $_SESSION['racer']['money'] += $track['prize'];
            $result_message = "恭喜你在 {$track['name']} 比赛中获胜，获得 {$track['prize']} 奖金！";
        } else {
            // 比赛失败
            $result_message = "很遗憾，你在 {$track['name']} 比赛中失败了。";
        }

        // 记录比赛结果
        $_SESSION['racer']['race_results'][] = [
            'track' => $track['name'],
            'result' => $random_result > $track['difficulty'] ? '获胜' : '失败'
        ];
    } else {
        $result_message = "油量不足，无法参加 {$track['name']} 的比赛。";
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟赛车手游戏</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        select {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }

        .result-list {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>

<body>
    <h1>模拟赛车手游戏</h1>
    <p>当前油量: <?php echo $_SESSION['racer']['fuel']; ?></p>
    <p>当前金钱: <?php echo $_SESSION['racer']['money']; ?></p>

    <?php if (isset($result_message)): ?>
        <div class="message <?php echo strpos($result_message, '恭喜')!== false? 'success' : 'error'; ?>">
            <?php echo $result_message; ?>
        </div>
    <?php endif; ?>

    <form method="post">
        <label for="track">选择赛道：</label>
        <select id="track" name="track">
            <?php foreach ($tracks as $key => $track): ?>
                <option value="<?php echo $key; ?>">
                    <?php echo $track['name']; ?>（油量消耗: <?php echo $track['fuel_cost']; ?>，奖金: <?php echo $track['prize']; ?>）
                </option>
            <?php endforeach; ?>
        </select>
        <button type="submit">开始比赛</button>
    </form>

    <div class="result-list">
        <h2>比赛成绩记录</h2>
        <?php if (empty($_SESSION['racer']['race_results'])): ?>
            <p>暂无比赛记录。</p>
        <?php else: ?>
            <ul>
                <?php foreach ($_SESSION['racer']['race_results'] as $result): ?>
                    <li><?php echo $result['track']; ?>：<?php echo $result['result']; ?></li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>
</body>

</html>